package ui.table;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;
import java.util.Calendar;

import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.JToggleButton;
import javax.swing.UIManager;
import javax.swing.table.TableCellRenderer;

import core.Utility;

import data.Event;
import data.EventDateTime;


public class EventCellRenderer implements TableCellRenderer
{

	
	 private final Color colour = UIManager.getColor("InternalFrame.activeTitleBackground");
	@Override
	public Component getTableCellRendererComponent(JTable table, Object value,
			boolean isSelected, boolean hasFocus, int row, int column) {
		if (value instanceof Event) {
			return renderEventLabel((Event)value, isSelected);
		} else if (value instanceof EventButton) {
			return (JToggleButton)value;//(JToggleButton)value;//new JLabel("Ha");
		}
		return null;
	}

	private Component renderEventButton(EventButton button) {
		return (JToggleButton)button;
	}

	private Component renderEventLabel(Event value, boolean isSelected) {
		Event event = (Event) value;

		String result = "";
		if (event.getName() != null) {
			result = event.getName();
		}
		EventDateTime defaultDateTime = new EventDateTime();
		if (!(event.getStart().getTime().equals(defaultDateTime))) {
			int hourTime;
			int minTime;
			int secTime;
			String Time = "";
			hourTime = event.getStart().get(Calendar.HOUR);
			minTime = event.getStart().get(Calendar.MINUTE);
			secTime = event.getStart().get(Calendar.SECOND);
			String suffix = event.getStart().getTime().getDisplayName(Calendar.AM_PM, Calendar.SHORT).toLowerCase();
			Time+=hourTime;
            if (minTime!=0) {
            	Time+="."+minTime;
            }
			result += " " + "at " + Time + suffix;
		}
		if(event.getDuration()!=0) {
			System.out.println(event.getDuration());
			int min[] = Utility.minutesToHour(event.getDuration());
			 
			if(min[1]==0&& min[0]!=0) {
				result+=" "+"for " +min[0]+"hrs";
			}
			else if (min[0]==0&&min[1]!=0)
			 {
			result+=" "+"for "+min[1]+"mins";
			}
			else {
				result+=" "+"for "+min[0]+"."+min[1]+"hrs";
			}
			
 		}
		if(!(event.getStart().getDate().equals(defaultDateTime))) {
			int day = event.getStart().get(Calendar.DAY_OF_MONTH);
			String month=event.getStart().getDate().getDisplayName(Calendar.MONTH,Calendar.SHORT);
			if (event.getDeadlineEvent()) {
				result += " by ";
			} else {
				result += " on ";
			}
			result += day + Utility.getDaySuffix(day) + " " + month;
			int year = event.getStart().getDate().get(Calendar.YEAR);
			if (year != EventDateTime.current().get(Calendar.YEAR)) {
				result += ", " + year;
			}
		}
		if (event.getLabels() != null) {
			String label ="";
			for(int i=0;i<event.getLabels().size();i++) {
				label+= " @"+event.getLabels().get(i);
			}
			result += " "+ label;
		}
		JLabel eventLabel = new JLabel();
		eventLabel.setAutoscrolls(true);
		eventLabel.setText(result);
       
		Color HIGHLIGHT_COLOR = new Color(0, 0, 139);
		eventLabel.setOpaque(true);
		eventLabel.setFont(new Font("Arial", Font.BOLD, 14));
		if(isSelected) {
			eventLabel.setBackground(colour);
			eventLabel.setForeground(Color.black);
		} else{
			eventLabel.setBackground(Color.white);
			eventLabel.setForeground(Color.black);
		 }

		return eventLabel;
	}
}
